#ifndef VSFTPD_MONITOR_H
#define VSFTPD_MONITOR_H

#include "../ftpmonitorloop.h"

/*
 *
 *
*/
class VsftpdMonitor : public FTPMonitorLoop
{
	public:

		/*
		 *
		 */
		VsftpdMonitor(QString whoPath,bool useSudo)
		:FTPMonitorLoop()
		{
			createExecCommand(useSudo,whoPath);
		};

		//ftp      18677 18675  0 09:30 ?        00:00:00 vsftpd: 10.20.1.244/ftp: RETR /uploads/archlinux-2009.02-core-i686.iso
		//ftp      18699 18697  0 09:30 ?        00:00:00 vsftpd: 10.20.1.244/ftp: IDLE 
		void processStdOut(QString stdout)
		{
			QRegExp rx;
			QVector<QStringList> listOfClients;
			QStringList clientRow;
			
			QStringList processList = QString(stdout).split("\n");
			
			QStringList::Iterator it = processList.begin();
			if(it == processList.end())
				return;
			
			for ( ; it != processList.end(); ++it ){
				rx.setPattern("^(\\S+).*(\\d+).*vsftpd: (.*)/.*:(.*) (.*)$");
				if (rx.indexIn(it->trimmed()) != -1){
					clientRow.clear();
					clientRow << rx.cap(2) << rx.cap(1)  << rx.cap(5) << rx.cap(4) << rx.cap(3);
					listOfClients.push_back(clientRow);
				}
			}
			
			searchForClientsStateChange(listOfClients);
		}
	
		void createExecCommand(bool,QString)
		{
// 			if (whoPath=="")
				mFtpWhoCommand.bin = "/bin/ps";
// 			else
// 				mFtpWhoCommand.bin = whoPath + "ps";

			mFtpWhoCommand.args << "-fe";

/*			if (useSudo==true)
			{
	      		mFtpWhoCommand.args.prepend(mFtpWhoCommand.bin);
				mFtpWhoCommand.bin="sudo";
			}*/
		}
		
		/*
		 *
		 */
		bool ftpClientDiff(QStringList newClientRow,QStringList oldClientRow)
		{
			if (newClientRow.at(2) != oldClientRow.at(2) || newClientRow.at(3) != oldClientRow.at(3))
				return true;
			else
				return false;
		}
		
		/*
		 *
		 */
		QStringList headerLabels()
		{
			return QStringList() << "PID" << "Login" << "State" << "File" << "Host";
		}

		/*
		 *
		 */
		FtpServerType ftpServerType()
		{
			return FTPMonitorLoop::vsftpd;
		}

		/*
		 *
		 */
		QString ftpdServerName()
		{
			return QString("vsftpd");
		}
};

#endif
